package com.woniuxy.carlife.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.param.CouponParam;
import com.woniuxy.carlife.param.FindCouponParam;
import com.woniuxy.carlife.domain.Coupon;
import com.woniuxy.carlife.service.CouponService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * @author fx
 * @ClassName:CouponController
 * @Description: 优惠券控制类
 * @date 2021/4/6 20:00
 **/

@RestController
@RequestMapping("/coupon")
@Api(tags = "优惠券接口信息")
@Slf4j
public class CouponController {

    @Resource
    private CouponService couponService;

    /**
     * 功能描述〈新增优惠券〉
     * @author:fx
     * @param: @RequestBody Coupon coupon:优惠券实体类
     * @return: Result
     * @Date 2021/4/6 22:51
     * @throws
     */
    @PostMapping("/platformmanage/coupon")
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "新增优惠券",notes = "<span style='color:red;'>用来新增优惠券</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "修改成功"),
            @ApiResponse(code=20001,message = "修改失败")
    })
    //@ApiImplicitParams用于描述接口参数
//    @ApiImplicitParams({
//            //dataType:参数类型
//            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
//            @ApiImplicitParam(name = "id",value = "优惠券类别id",dataType = "Integer",paramType = "query",example = "1"),
//    })
    public Result saveCoupon(@RequestBody @ApiParam CouponParam coupon) throws Exception {
        log.info("saveCoupon开始:参数:{}",coupon);
        //判断传入参数的情况
        if (coupon.getCouponCategoryId()<=0||coupon.getCouponName()==null|| coupon.getCouponName().equals("string")){
            log.info("saveCoupon结束:查询失败:{}","参数错误");
            return new Result(false,StatusCode.PARAMERROR,"参数错误");
        }
        int i = couponService.saveCoupon(coupon);
        if (i==1) {
            log.info("saveCoupon结束:{}","新增成功");
            return new Result(true, StatusCode.OK,"新增成功");
        }else if (i==-1){
            log.info("saveCoupon结束:{}","新增失败，该名称已存在");
            return new Result(false, StatusCode.ERROR,"新增失败，该名称已存在");
        }else if (i==0){
            log.info("saveCoupon结束:{}","新增失败");
            return new Result(false, StatusCode.ERROR,"新增失败");
        }
        return null;
    }

    /**
     * 功能描述〈根据优惠券查询的VO类查询指定优惠券信息,可做多条件查询〉
     * @author:fx
     * @param: @RequestBody FindCouponParam couponParam:优惠券条件查询VO类
     * @return: Result
     * @Date 2021/4/6 23:01
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "多条件查询优惠券",notes = "<span style='color:red;'>用来多条件查询优惠券</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功"),
            @ApiResponse(code=20001,message = "查询失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @GetMapping("/ordinary/coupon")
    public Result findCouponByCouponId(@ApiParam FindCouponParam couponParam){
        log.info("findCouponByCouponId开始:参数{}",couponParam);
        List<Coupon> couponByCondition = couponService.findCouponByCondition(couponParam);
        if (ObjectUtils.isEmpty(couponByCondition)){
            log.info("findCouponByCouponId结束:{}","查询失败，没有数据");
            return new Result(false, StatusCode.ERROR,"查询失败，没有数据");
        }
        log.info("findCouponByCouponId结束:{}","查询成功");
        return new Result(true, StatusCode.OK,"查询成功",couponByCondition);
    }

    /**
     * 功能描述〈根据优惠券id修改库存量并同时判断修改优惠券状态和修改时间，
     *              用户领取优惠券时调用实现类的减少方法，这里不在控制类展示了〉
     * @author:fx
     * @param: Integer id:优惠券id
     * @return: Result
     * @Date 2021/4/6 23:03
     * @throws
     */
//    @ApiOperation(value = "根据优惠券id修改库存量",notes = "<span style='color:red;'>根据优惠券id修改库存量并同时判断修改优惠券状态和修改时间</span>")
//    //@ApiResponses用于描述响应状态信息
//    @ApiResponses({
//            @ApiResponse(code = 20000,message = "修改成功"),
//            @ApiResponse(code=20001,message = "修改失败"),
//            @ApiResponse(code=30001,message = "参数错误")
//    })
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "优惠券id",dataType = "Integer",paramType = "query",example = "1"),
//    })
//    @PutMapping("/ordinary/couponnum")
//    public Result updateCouponNumByCouponId(Integer id)throws Exception{
//        log.info("updateCouponNumByCouponId开始:参数{}",id);
//        if (id==null||id<=0){
//            log.info("updateCouponNumByCouponId结束:查询失败:{}","参数错误");
//            return new Result(false,StatusCode.PARAMERROR,"参数错误");
//        }
//        if (couponService.updateCouponNumByCouponIdFromRedis(id)) {
//            log.info("updateCouponNumByCouponId结束:{}","修改数量成功");
//            return new Result(true,StatusCode.OK,"修改数量成功");
//        }
//        log.info("updateCouponNumByCouponId结束:{}","修改数量失败");
//        return new Result(false,StatusCode.ERROR,"修改数量失败");
//    }

    /**
     * 功能描述〈根据优惠券id查询详情〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 18:57
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "根据优惠券id查询详情",notes = "<span style='color:red;'>根据优惠券id查询详情</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功",response = Coupon.class),
            @ApiResponse(code=20001,message = "查询失败"),
            @ApiResponse(code=30001,message = "参数错误"),
            @ApiResponse(code=00000,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId",value = "优惠券id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("/ordinary/couponid")
    public Result findCouponInfoByCouponId(Integer couponId) {
        log.info("findCouponInfoByCouponId开始:参数{}",couponId);
        if (couponId==null||couponId<=0){
            log.info("findCouponInfoByCouponId结束:查询失败:{}","参数错误");
            return new Result(false,StatusCode.PARAMERROR,"参数错误");
        }
        Coupon couponInfoByCouponId = couponService.findCouponInfoByCouponId(couponId);
        if (ObjectUtils.isEmpty(couponInfoByCouponId)){
            log.info("findCouponInfoByCouponId结束:查询失败:{}","查询失败");
            return new Result(false,StatusCode.NULLDATA,"查询指定优惠券失败");
        }
        log.info("findCouponInfoByCouponId结束:查询成功:返回:{}",couponInfoByCouponId);
        return new Result(true,StatusCode.OK,"查询指定优惠券成功",couponInfoByCouponId);
    }
}

